package com.ruoyi.file.service.storage.impl.aws;

import com.ruoyi.file.domain.SysStorageConfig;
import com.ruoyi.file.domain.vo.UploadTokenVO;
import com.ruoyi.file.enums.StorageTypeEnum;
import com.ruoyi.file.service.storage.base.BaseS3StorageServiceImpl;
import com.ruoyi.file.utils.FilePathUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.time.Duration;

/**
 * AWS S3存储服务实现
 * 
 * @author ruoyi
 */
@Slf4j
@Service("awsS3StorageService")
public class AwsS3StorageServiceImpl extends BaseS3StorageServiceImpl {

    @Override
    protected StorageTypeEnum getStorageType() {
        return StorageTypeEnum.AWS;
    }

    @Override
    public boolean shouldUsePathStyle() {
        return false; // AWS S3 使用虚拟主机风格
    }

    @Override
    protected boolean shouldSetContentType() {
        return true; // AWS S3 设置content-type
    }

    @Override
    protected String buildFileUrl(String protocol, String endpoint, String bucketName, String filePath, StorageTypeEnum storageType) {
        // AWS S3使用虚拟主机风格: bucket.endpoint/filePath
        return String.format("%s://%s.%s/%s", protocol, bucketName, endpoint, filePath);
    }

    @Override
    public UploadTokenVO generateUploadToken(String fileName, SysStorageConfig config) {
        // AWS 优先使用 STS 临时凭证
        String filePath = FilePathUtils.generateFilePathWithDate(fileName);
        Duration expiration = Duration.ofSeconds(config.getTokenExpireTime());
        return generateAwsStsToken(filePath, config, expiration);
    }

    @Override
    public UploadTokenVO generateUploadToken(String fileName, SysStorageConfig config, String dirPath) {
        // AWS 优先使用 STS 临时凭证，支持自定义目录路径
        return super.generateUploadToken(fileName, config, dirPath);
    }
}
